package com.naizhuai.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 拼接json字符串
 * Created by code_han on 2018/3/5.
 */

public class UidUtil {

    public String UID_KEY = "uid";

    private List<String> uids;

    private boolean hasKey = true;

    public UidUtil() {
        uids = new ArrayList<>();
    }

    public UidUtil(List<String> strs) {
        uids = new ArrayList<>();
        for (String str : strs)
            add(str);
    }
    public UidUtil(List<String> strs,String uidKey) {
        UID_KEY=uidKey;
        uids = new ArrayList<>();
        for (String str : strs)
            add(str);
    }

    public UidUtil(boolean hasKey, String[] strs) {
        this.hasKey = hasKey;
        uids = new ArrayList<>();
        for (String str : strs)
            add(str);
    }

    public UidUtil(boolean hasKey) {
        this.hasKey = hasKey;
        uids = new ArrayList<>();
    }

    public UidUtil(boolean hasKey, List<String> strs) {
        this.hasKey = hasKey;
        uids = new ArrayList<>();
        for (String str : strs)
            add(str);
    }


    public UidUtil(String key, String... strs) {
        UID_KEY = key;
        uids = new ArrayList<>();
        for (String str : strs)
            add(str);
    }

    public UidUtil(String str) {
        uids = new ArrayList<>();
        add(str);
    }

    public void add(String uid) {
        if (!uids.contains(uid))
            uids.add(uid);
    }

    public void remove(int index) {
        if (uids.get(index) != null)
            uids.remove(index);
    }

    public void remove() {
        if (uids.size() > 0)
            uids.remove(uids.size() - 1);
    }

    public void clear() {
        uids.clear();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (uids.size() == 0)
            return "[]";
        sb.append("[");
        for (String str : uids) {
            if (!hasKey)
                sb.append("\"").append(str).append("\",");
            else
                sb.append("{\"").append(UID_KEY).append("\":").append("\"").append(str).append("\"},");
        }
        sb.deleteCharAt(sb.length() - 1).append("]");

        return sb.toString();
    }
}
